//
//  RHMePlantListCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHMePlantListCell.h"
#import "RHTools.h"

@interface RHMePlantListCell()
@property (nonatomic ,strong) QMUILabel *plantNameLab;//电站名称

@property (nonatomic ,strong) QMUILabel *deviceCountLab;//设备数量

@property (nonatomic ,strong) QMUILabel *statusLab;//通信状态

@property (nonatomic ,strong) QMUILabel *alarmFaultCountLab;//故障告警数量

@property (nonatomic ,strong) QMUILabel *chgStatusLab;//充
@property (nonatomic ,strong) QMUILabel *dchgStatusLab;//放
@property (nonatomic ,strong) QMUILabel *standbyStatusLab;//待

@property (nonatomic ,strong) QMUILabel *todayDchgLab;//今日放电量

@property (nonatomic ,strong) QMUILabel *todayEarningLab;//今日收益


@end
@implementation RHMePlantListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHPlantModel *)model
{
    _model = model;
    self.plantNameLab.text = model.plantName;
    self.deviceCountLab.text     = [NSDecimalNumber decimalStringWithDoubleString:model.deviceNumber nullStr:@"--"];
    self.statusLab.text          = [NSDecimalNumber decimalStringWithDoubleString:model.monitorNumber nullStr:@"--"];
    self.alarmFaultCountLab.text = [NSDecimalNumber decimalStringWithDoubleString:model.alarmNumber nullStr:@"--"];
    NSString *today_dischg       = [NSDecimalNumber decimalStringWithDoubleString:model.todayDischarge nullStr:@"--"];
    self.todayDchgLab.text       = [NSString stringWithFormat:@"%@ kWh",today_dischg];
    
    //货币种类 0:人民币(CNY)  1:美元(USD) 2:欧元(EUR) 3:日元(JPY) 4:英镑(GBP)  5:澳元(AUD)
    NSString *currencyType = [RHTools getCurrentCurrencyType];
    NSString *today_income       = [NSDecimalNumber decimalStringWithDoubleString:model.yesterdayRevenue nullStr:@"--"];
    self.todayEarningLab.text = [NSString stringWithFormat:@"%@ %@",today_income,currencyType];

    self.chgStatusLab.text = model.chargeNumber;
    self.dchgStatusLab.text = model.dischargeNumber;
    self.standbyStatusLab.text = model.waitingNumber;
}

- (void)setupUI{

    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    kWeakSelf;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];

    _plantNameLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    [bgView addSubview:_plantNameLab];
    [_plantNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    UIView *bgView2 = kView;
    bgView2.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    [bgView2 rounded:8];
    [bgView addSubview:bgView2];
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.plantNameLab.mas_bottom).offset(15);
        make.height.offset(90);
    }];
    
    NSArray *titles = @[@"设备数量".rh_localized,@"通信状态".rh_localized,@"故障告警".rh_localized];
    NSArray *icons = @[@"plant_asset_dev_count",@"plant_asset_status",@"plant_asset_alarm_count"];
    NSMutableArray *arr = [NSMutableArray array];
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [self itemViewWithTitle:title icon:icons[idx] index:idx];
        [bgView2 addSubview:itemView];
        [arr addObject:itemView];
    }];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
    }];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
    
    QMUILabel *chgTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    chgTitleLab.numberOfLines = 2;
    chgTitleLab.text = @"充放状态".rh_localized;
    [bgView addSubview:chgTitleLab];
    
    
    __block UIView *lastView;
    NSArray *imageArr = @[@"plant_list_standby",@"plant_list_dchg",@"plant_list_chg"];
    [imageArr enumerateObjectsUsingBlock:^(NSString*  _Nonnull icon, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *chgStateView = kView;
        [bgView addSubview:chgStateView];
        QMUILabel *countLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
        countLab.text = @"0";
        [chgStateView addSubview:countLab];
        
        UIImageView *img = [[UIImageView alloc]initWithImage:kImage(icon)];
        [chgStateView addSubview:img];
        
        [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(0);
            make.centerY.mas_offset(0);
        }];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_offset(0);
            make.size.equalTo(@(CGSizeMake(20, 20)));
            make.right.equalTo(countLab.mas_left).offset(-8);
        }];
        
        if (lastView) {
            [chgStateView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(lastView.mas_left).offset(-10);
                make.centerY.equalTo(lastView);
            }];
        }else{
            [chgStateView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_offset(-15);
                make.centerY.equalTo(chgTitleLab);
            }];
        }
        
        lastView = chgStateView;
        if (idx == 0) {
            self.standbyStatusLab = countLab;
        }else if(idx == 1){
            self.dchgStatusLab = countLab;
        }else{
            self.chgStatusLab = countLab;
        }
    }];
    
    [chgTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView2.mas_bottom).offset(15);
        make.left.mas_offset(15);
        make.right.equalTo(lastView.mas_left);
    }];
    
    
    
    QMUILabel *dchgTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    dchgTitleLab.text = @"今日放电".rh_localized;
    [bgView addSubview:dchgTitleLab];
    [dchgTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(chgTitleLab.mas_bottom).offset(15);
        make.left.mas_offset(15);
       
    }];
    
    QMUILabel *dchgContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentRight];
    self.todayDchgLab = dchgContentLab;
    dchgContentLab.text = @"--";
    [bgView addSubview: dchgContentLab];
    [dchgContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dchgTitleLab.mas_right);
        make.right.mas_offset(-15);
        make.top.equalTo(dchgTitleLab);
    }];
    
    QMUILabel *earnTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    earnTitleLab.text = @"昨日收益".rh_localized;
    [bgView addSubview:earnTitleLab];
    [earnTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dchgTitleLab.mas_bottom).offset(15);
        make.left.equalTo(dchgTitleLab);
        make.bottom.mas_offset(-15);
    }];
    
    QMUILabel *earnContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentRight];
    self.todayEarningLab = earnContentLab;
    earnContentLab.text = @"--";
    [bgView addSubview: earnContentLab];
    [earnContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(earnTitleLab.mas_right);
        make.right.mas_offset(-15);
        make.top.equalTo(earnTitleLab);
    }];
}

-(UIView*)itemViewWithTitle:(NSString*)title icon:(NSString*)icon index:(NSInteger)index
{
    UIView *bgView = kView;
    QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    titleLab.numberOfLines = 2;
    titleLab.text = title;
    [bgView addSubview:titleLab];
    
    
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.width.height.offset(32);
        make.centerX.mas_offset(0);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.top.equalTo(img.mas_bottom).offset(0);
        make.bottom.mas_offset(-3);
    }];
    
    QMUILabel *countLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    countLab.backgroundColor = kCOLOR_HEX(@"ffffff");
    countLab.layer.cornerRadius = 8;
    countLab.layer.masksToBounds = YES;
    [bgView addSubview:countLab];
    countLab.text = @"16";
    [countLab sizeToFit];
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(-5);
        make.top.equalTo(img);
        make.height.offset(18);
        make.width.offset(countLab.width+10);
    }];
    
    if (index == 0) {
        self.deviceCountLab = countLab;
    }
    if (index == 1) {
        self.statusLab = countLab;
    }
    if (index == 2) {
        self.alarmFaultCountLab = countLab;
    }
    return bgView;
}

@end
